package ginroutes

import (
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"local.public/api"
	_ "local.public/docs"
	"local.public/innerpkg/logger"
	"local.public/middleware"
)

var g *gin.Engine

// 路由初始化
func InitRouter() *gin.Engine {

	g = gin.New() // 创建不带中间件的路由
	g.GET("/doc/public/swagger/*any", func(c *gin.Context) {
		ginSwagger.DisablingWrapHandler(swaggerFiles.Handler, "SWAGGER")(c)
	})
	// 中间件
	g.Use(middleware.AccessLog) // 访问中间件
	g.Use(middleware.Cors)      // 跨域中间件
	g.Use(middleware.Exception) // 异常中间件
	g.Use(gin.Recovery())

	var vGroup *gin.RouterGroup
	vGroup = g.Group("api")
	{
		vSchiGroup := vGroup.Group("public", middleware.HeaderAuth)
		{
			api.BindPingPongRouters(vSchiGroup) // PingPong接口,GET请求
		}

	}

	//vFileGroup := g.StaticFS("/"+datastore.GetConfig().StaticFile.Alias, http.Dir(datastore.GetConfig().StaticFile.Path))
	//vFileGroup.Use(middleware.FileAccessLog)

	logger.Debug("路由初始化完成")
	return g
}
